package cn.luowb.checkchat.service.impl;

import cn.hutool.crypto.digest.BCrypt;
import cn.luowb.checkchat.dao.entity.UserDO;
import cn.luowb.checkchat.dao.mapper.UserMapper;
import cn.luowb.checkchat.dto.req.UserLoginRequest;
import cn.luowb.checkchat.dto.req.UserRegisterRequest;
import cn.luowb.checkchat.dto.req.UserUpdateRequest;
import cn.luowb.checkchat.service.UserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;

/**
 * @author heimenkyou
 * @description 针对表【user(用户)】的数据库操作Service实现
 * @createDate 2025-09-01 19:07:09
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, UserDO>
        implements UserService {
    @Autowired
    private UserMapper userMapper;

    @Override
    @Transactional
    public UserDO register(UserRegisterRequest request) {
        // 检查账号是否已存在
        UserDO existingUser = userMapper.selectByLoginId(request.getLoginId());
        if (existingUser != null) {
            throw new RuntimeException("账号已存在");
        }

        // 创建用户实体
        UserDO user = new UserDO();
        user.setLoginId(request.getLoginId());
        // 密码加密
        user.setPasswordHash(BCrypt.hashpw(request.getPassword(), BCrypt.gensalt()));
        user.setNickName(request.getNickname());
        user.setAvatar(request.getAvatar());
        user.setIntro(request.getIntro());
        user.setCreateTime(LocalDateTime.now());
        user.setUpdateTime(LocalDateTime.now());

        // 保存用户
        userMapper.insert(user);
        return user;
    }


    /**
     * 校验登录信息（用户名密码）
     */
    public UserDO verifyLogin(UserLoginRequest request) {
//        // 1. 根据 loginId 查询用户
//        UserDO user = userMapper.selectByLoginId(request.getLoginId());
//        if (user == null) {
//            throw new RuntimeException("用户名或密码错误");
//        }
//        // 2. 校验密码（实际项目需用 BCrypt 等算法比对哈希）
//        if (!user.getPasswordHash().equals(request.getPassword())) { // 示例：实际需替换为哈希校验
//            throw new RuntimeException("用户名或密码错误");
//        }
        UserDO user = userMapper.selectByLoginId(request.getLoginId());
        if (user == null || !BCrypt.checkpw(request.getPassword(), user.getPasswordHash())) {
            throw new RuntimeException("用户名或密码错误");
        }

        // 3. 可更新最后登录时间/IP等信息
        return user;
    }


    @Override
    public UserDO getUserInfo(Long uid) {
        UserDO user = userMapper.selectById(uid);
        if (user == null) {
            throw new RuntimeException("用户不存在");
        }
        return user;
    }

    @Override
    @Transactional
    public void updateUserInfo(Long uid, UserUpdateRequest request) {
        UserDO user = userMapper.selectById(uid);
        if (user == null) {
            throw new RuntimeException("用户不存在");
        }

        // 更新用户信息
        if (request.getNickname() != null) {
            user.setNickName(request.getNickname());
        }
        if (request.getAvatar() != null) {
            user.setAvatar(request.getAvatar());
        }
        if (request.getIntro() != null) {
            user.setIntro(request.getIntro());
        }
        user.setUpdateTime(LocalDateTime.now());

        userMapper.updateById(user);
    }


}




